#include "KeyOperationsManager.h"
#include "ActionCreateKey.h"

using namespace kryptos::actions;
using namespace kryptos::entities;

namespace kryptos { namespace controller {

KeyOperationsManager::KeyOperationsManager()
{
}

KeyOperationsManager& KeyOperationsManager::manager()
{
    static KeyOperationsManager instance;
    return instance;
}

void KeyOperationsManager::createKey(QMap<QString, QString> params)
{
    if (params.contains("Name") && params.contains("Email") && params.contains("Passphrase"))
    {
        KeyInfo keyinfo(KeyInfo::RSA,
                        params.value("Name").toStdString(),
                        params.value("Email").toStdString(),
                        params.value("Comment").toStdString(),
                        params.value("Passphrase").toStdString(),
                        1024);

        m_currentAction.reset(new ActionCreateKey(keyinfo));

        // connect action signals to report status
        connect(m_currentAction.data(), SIGNAL(actionEnded(bool)), this, SLOT(slotCreateKeyFinished(bool)));

        m_currentAction->execute();
    }
    else
    {
        //error signal
    }
}

void KeyOperationsManager::shareKey(QMap<QString, QString> params)
{

}

void KeyOperationsManager::slotCreateKeyFinished(bool success)
{
    emit keyOperationEnded();
}

}}
